﻿using BLL;
using Model;
using System;
using Thksoft.Modal.Page;

namespace Thksoft.Modal
{
    public partial class EditBank : FormPage
    {
        private Sys_BankBLL bankBLL = new Sys_BankBLL();

        protected override void BindPage()
        {
            if (FORM_IFEDIT)
            {
                var entity = bankBLL.Find(Convert.ToDecimal(FORM_KEY));
                FORM_ID.Text = entity.N_ID.ToString();
                FORM_NAME.Text = entity.C_NAME;
            }
            else
            {
                FORM_DIV.Visible = false;
                FORM_NAME.Text = "";
            }
        }

        protected override void SaveClick(object sender, EventArgs e)
        {
            if (FORM_NAME.Text.TrimEnd() == "")
            {
                MessageBox.Show(this, "银行名称不能为空");
                return;
            }
            if (FORM_IFEDIT)
            {
                var entity = bankBLL.Find(Convert.ToDecimal(FORM_KEY));
                if (entity.C_NAME != FORM_NAME.Text.TrimEnd())
                {
                    if (bankBLL.GetCount("SELECT COUNT(*) FROM SYS_BANK WHERE C_NAME = '" + FORM_NAME.Text.TrimEnd() + "'") > 0)
                    {
                        MessageBox.Show(this, "该银行名称已存在，不能重复添加");
                        return;
                    }
                }
                entity.C_NAME = FORM_NAME.Text.TrimEnd();

                bool istrue = bankBLL.Update(entity, CurrentUserName);

                if (istrue)
                {
                    MessageBox.Show(this, "修改成功");
                }
                else
                {
                    MessageBox.Show(this, "修改失败");
                }
            }
            else
            {
                var entity = new SYS_BANK();
                if (bankBLL.GetCount("SELECT COUNT(*) FROM SYS_BANK WHERE C_NAME = '" + FORM_NAME.Text.TrimEnd() + "'") > 0)
                {
                    MessageBox.Show(this, "该银行名称已存在，不能重复添加");
                    return;
                }

                entity.C_NAME = FORM_NAME.Text.TrimEnd();

                if (bankBLL.Insert(entity, CurrentUserName))
                {
                    MessageBox.ShowAndRedirect(this, "新增成功", "/Modal/EditBank.aspx?IF_EDIT=TRUE&ID=" + entity.N_ID);
                }
                else
                {
                    MessageBox.Show(this, "新增失败");
                }
            }
        }

        protected override void UpdateTitle()
        {
            if (FORM_IFEDIT)
            {
                MessageBox.ResponseScript(this, "FORM_UPDATE_TITLE(\"编辑银行设置\");");
            }
            else
            {
                MessageBox.ResponseScript(this, "FORM_UPDATE_TITLE(\"添加银行设置\");");
            }
        }

        protected override void UpdateFormSize()
        {
            if (FORM_IFEDIT)
            {
                MessageBox.ResponseScript(this, "FORM_SIZE(827,310);");
            }
            else
            {
                MessageBox.ResponseScript(this, "FORM_SIZE(900,280);");
            }
        }
    }
}